/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.services;


import com.kcp.ko.fmtnmlib.tapestry.services.GenericEngineServiceAdapter;
import org.apache.commons.lang.StringUtils;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.json.JSONArray;
import org.apache.tapestry.json.JSONLiteral;
import org.apache.tapestry.json.JSONObject;
import org.apache.tapestry.util.ContentType;
import org.apache.tapestry.web.WebResponse;
import org.braintrain.domain.Answer;
import org.braintrain.domain.Question;

import javax.activation.MimetypesFileTypeMap;
import java.io.IOException;
import java.io.OutputStream;

/**
 * AnswerHitService generates a JSON response that shows the responses related to a question
 * User: Robert H. Engelhardt
 * Date: 4/15/11
 * Time: 10:53 AM
 */
public class AnswerHitService extends GenericEngineServiceAdapter {

    private MimetypesFileTypeMap mimeTypes = new MimetypesFileTypeMap();

    public static final String QUESTION_ID = "questionId";
    public static final String NULL_LABEL = "nullLabel";

    public static final String SERVICE_NAME = "answerHitService";

    public String getName() {
        return SERVICE_NAME;
    }

    public void service(IRequestCycle cycle) throws IOException {

        WebResponse response = cycle.getInfrastructure().getResponse();

        Long questionId = unsqueezeParameter(cycle, Long.class, QUESTION_ID);

        String nullLabel = null;
        if(cycle.getParameter(NULL_LABEL) != null) {
            nullLabel = unsqueezeParameter(cycle, String.class, NULL_LABEL);
        }

        Question question = getHibernateSearchService().getById(Question.class, questionId);

        response.setHeader("Cache-Control", "no-cache");

        OutputStream out = response.getOutputStream(new ContentType(mimeTypes.getContentType("application/json")));

        JSONArray jsonArray = new JSONArray();
        for(Answer answer: question.getAnswers()) {
            JSONObject jsonPair = new JSONObject();

            int answerCount = answer.getAnswerLog().size();
            String answerString = answer.getAnswer();

            if(StringUtils.isBlank(answerString)) {
                jsonPair.put(new JSONLiteral("label"), nullLabel + " (" + answerCount + ")" );
            } else {
                jsonPair.put(new JSONLiteral("label"), answerString.trim() + " (" + answerCount + ")" );
            }


            jsonPair.put(new JSONLiteral("data"), answerCount);
            jsonArray.put(jsonPair);
        }

        out.write(jsonArray.toString().getBytes());
    }

}
